//
//  AddTransactionController.m
//  LuCaiZhu
//
//  Created by stella Ma on 2018/12/20.
//

#import "HandleNetProduct+Transaction.h"
#import "UIColor+NN.h"
#import "UserModel.h"
#import "Header.h"
#import "SearchController.h"
#import "BookingModel.h"
#import "CKPuslichTool.h"
#import "RenGouOrderCell.h"
#import "AddRenGouCell.h"
#import "DeleteRenGouController.h"
#import "BookOrderMode.h"
#import "SigningProcessController.h"
#import "UIAlertView+ActionBlock.h"
#import "EditTransactionController.h"
#import "AddTransactionController.h"
#import "RansomAlertController.h"
#import "InspectController.h"
#import "ConsultantController.h"

@interface AddTransactionController (){
    int type;
    NSString *searchName;
}
@property (weak, nonatomic) IBOutlet UITableView *mTableView;

@property (weak, nonatomic) IBOutlet UIView *noData;
@property (weak, nonatomic) IBOutlet UILabel *topLeft;
@property (weak, nonatomic) IBOutlet UILabel *topRight;
@property (weak, nonatomic) IBOutlet UIButton *leftBt;
@property (weak, nonatomic) IBOutlet UIButton *rightBt;
@property(strong,nonatomic)NSArray *dataArray;
@end

@implementation AddTransactionController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"追加认购管理";
    searchName = @"";
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    [rightBtn setImage:[UIImage imageNamed:@"sousuoW"] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [rightBtn addTarget:self action:@selector(rightButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
    self.mTableView.estimatedRowHeight=110;
    
    self.mTableView.rowHeight = UITableViewAutomaticDimension;
    type = 0;
    [self getAddDatas];
}

-(void)rightButton:(UIBarButtonItem *)barBt{
    //去搜索页面
    SearchController *vc=(SearchController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"SearchController"];
    vc.returnSearchContent = ^(NSString *searchContent) {
        
        self.noData.hidden=YES;
        searchName = searchContent;
        if (type == 0) {
            [self getAddDatas];
        }else{
            [self getOrderlistDatas];
        }
    };
    vc.whereFrom = @"Report";
    [self.navigationController pushViewController:vc animated:YES];
}


- (IBAction)changType:(UIButton *)sender {
    
    if (sender.tag == 0) {
        self.topLeft.backgroundColor = [UIColor colorWithHexString:@"BB3535"];
        self.leftBt.selected = YES;
        self.topRight.backgroundColor = [UIColor clearColor];
        self.rightBt.selected = NO;
        type = 0;
        [self getAddDatas];
    }else{
        self.topRight.backgroundColor = [UIColor colorWithHexString:@"BB3535"];
        self.rightBt.selected = YES;
        self.topLeft.backgroundColor = [UIColor clearColor];
        self.leftBt.selected = NO;
        type = 1;
        [self getOrderlistDatas];
    }
//    [self.mTableView reloadData];
}

- (void)getAddDatas {
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];
    [HandleNetProduct getAddRenGouListWithCustomerId:mid searchName:searchName Success:^(NSArray * _Nonnull data) {
        if (data.count > 0) {
            _dataArray = data;
            _noData.hidden = YES;
        }else{
            _noData.hidden = NO;
        }
        [_mTableView reloadData];
    } Failure:^(NSString * _Nonnull errorInfo) {
        
    }];
}

- (void)getOrderlistDatas {
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];
    [HandleNetProduct getRenGouOrderListWithCustomerId:mid searchName:searchName Success:^(NSArray * _Nonnull data) {
        if (data.count > 0) {
            _dataArray = data;
            _noData.hidden = YES;
        }else{
            _noData.hidden = NO;
        }
        [_mTableView reloadData];
    } Failure:^(NSString * _Nonnull errorInfo) {
        
    }];
}

#pragma mark - 作废
- (void)invalidTheOrder:(UIButton *)sender {
    DeleteRenGouController *vc = (DeleteRenGouController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"DeleteRenGouController"];
    [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [vc setModalPresentationStyle:UIModalPresentationOverFullScreen];
    vc.isReminder = NO;
    vc.returnBlock = ^{
        BookOrderMode *orderMode = [BookOrderMode mj_objectWithKeyValues:_dataArray[sender.tag]];
        [HandleNetProduct BookingRevokeforBookingID:orderMode.BookingId Success:^(NSDictionary * _Nonnull data) {
            //作废成功后怎么处理
            [self getOrderlistDatas];
        } Failure:^(NSString * _Nonnull errorInfo) {
            
        }];
    };
    
    [self presentViewController:vc animated:YES completion:nil];
    
//    NSString *msg = @"您确认要作废该笔认购订单么？";
//    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"消息提醒" message:msg delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是",nil];
//    alert.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
//        if (buttonIndex == 1) {
//           
//        }
//    };
//    
//    [alert show];
}

#pragma mark - 弹框--不能操作
- (void)reminderUser {
    DeleteRenGouController *vc = (DeleteRenGouController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"DeleteRenGouController"];
    [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [vc setModalPresentationStyle:UIModalPresentationOverFullScreen];
    vc.isReminder = YES;
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark - 弹框--去签约流程
- (void)reminderUser2:(NSDictionary *)dic {
    DeleteRenGouController *vc = (DeleteRenGouController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"DeleteRenGouController"];
    [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [vc setModalPresentationStyle:UIModalPresentationOverFullScreen];
    vc.isYuYue = YES;
    vc.yuyueText = dic[@"Message"];
    vc.returnBlock = ^{
        [self goSigningController:dic];
    };
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)goSigningController:(NSDictionary *)dic {
    SigningProcessController *vc = (SigningProcessController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"SigningProcessController"];
    vc.bookingId = dic[@"BookingId"];
    vc.returnBlock = ^{
        [self changType:_rightBt];
    };
    vc.SigningStatus = @"A";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 追加认购、编辑
- (void)GoEditView:(UIButton *)sender {
    EditTransactionController *vc = (EditTransactionController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"EditTransactionController"];
    
    if (type == 0) {
        //新建
        vc.isEidt = NO;
        BookingModel *bookM = [BookingModel mj_objectWithKeyValues:_dataArray[sender.tag]];
        if ([bookM.IsButton isEqualToString:@"1"]) {
            [HandleNetProduct huiAlertRequest:bookM.BookingId Success:^(NSArray * _Nonnull data) {
                
            } Failure:^(NSString * _Nonnull errorInfo) {
                [self huiAlertAction:errorInfo and:bookM top:NO];
            }];
//            [self reminderUser];
            return;
        }
        if (bookM.Message) {
            [self huiAlertAction:bookM.Message and:bookM top: YES];
            return;
        }
        else {
            vc.bookMode = bookM;
            vc.returnAddBlock = ^(NSDictionary * _Nonnull data) {//追加成功返回
                [self changType:_rightBt];
                if ([data[@"Type"] isEqualToString:@"在线签约"]) {
                    //弹去签约提示框
                    [self reminderUser2:data];
                }
            };
        }
    }else{
        //编辑
        vc.isEidt = YES;
        BookOrderMode *orderM = [BookOrderMode mj_objectWithKeyValues:_dataArray[sender.tag]];
        if ([orderM.IsButton isEqualToString:@"1"]) {
            [self reminderUser];
            return;
        }
        vc.orderMode = orderM;
        vc.returnAddBlock = ^(NSDictionary * _Nonnull data) {//追加成功返回
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:data];
            [dic setObject:orderM.BookingId forKey:@"BookingId"];
            [self changType:_rightBt];
            if ([data[@"Type"] isEqualToString:@"在线签约"]) {
                //弹去签约提示框
                [self reminderUser2:dic];
            }
        };
    }
   
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)huiAlertAction:(NSString *)msg and:(BookingModel *)model top:(BOOL)isTop {
    RansomAlertController *vc = (RansomAlertController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"RansomAlertController"];
    if (isTop) {
        vc.rightStr = @"我知道了";
    }
    [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [vc setModalPresentationStyle:UIModalPresentationOverFullScreen];
    vc.yuyueText = [msg stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    if (msg.length == 0) {
        EditTransactionController *vc = (EditTransactionController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"EditTransactionController"];
        vc.bookMode = model;
        vc.returnAddBlock = ^(NSDictionary * _Nonnull data) {//追加成功返回
            [self changType:_rightBt];
            if ([data[@"Type"] isEqualToString:@"在线签约"]) {
                //弹去签约提示框
                [self reminderUser2:data];
            }
        };
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    vc.returnBlock = ^(NSInteger type) {
        switch (type) {
            case 1:
            {
                InspectController *vc=(InspectController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"InspectController"];
                vc.productId = model.ProductId;
                vc.customerID = model.Accountid;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 2:
            {
                if (isTop) {
                    EditTransactionController *vc = (EditTransactionController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"EditTransactionController"];
                    vc.bookMode = model;
                    vc.returnAddBlock = ^(NSDictionary * _Nonnull data) {//追加成功返回
                        [self changType:_rightBt];
                        if ([data[@"Type"] isEqualToString:@"在线签约"]) {
                            //弹去签约提示框
                            [self reminderUser2:data];
                        }
                    };
                    [self.navigationController pushViewController:vc animated:YES];
                }
                else {
                    ConsultantController *vc = (ConsultantController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"ConsultantController"];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                
            }
                break;
        }
    };
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (type == 0) {
        AddRenGouCell *cell=[tableView dequeueReusableCellWithIdentifier:@"AddRenGouCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
        BookingModel *mode = [BookingModel mj_objectWithKeyValues:_dataArray[indexPath.section]] ;
        cell.bookMode = mode;
        cell.addButton.tag = indexPath.section;
        [cell.addButton addTarget:self action:@selector(GoEditView:) forControlEvents:UIControlEventTouchDown];
        return cell;
    }else{
        RenGouOrderCell *cell=[tableView dequeueReusableCellWithIdentifier:@"RenGouOrderCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
        BookOrderMode *mode = [BookOrderMode mj_objectWithKeyValues:_dataArray[indexPath.section]];
        cell.orderMode = mode;
        cell.editButton.tag = indexPath.section;
        [cell.editButton addTarget:self action:@selector(GoEditView:) forControlEvents:UIControlEventTouchDown];
        cell.invalidBt.tag = indexPath.section;
        [cell.invalidBt addTarget:self action:@selector(invalidTheOrder:) forControlEvents:UIControlEventTouchDown];
        return cell;
    }
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

@end
